/*******************************************************************************
 * Copyright (c) 2000, 2005 IBM Corporation and others.
 * All rights reserved. This program and the accompanying materials
 * are made available under the terms of the Eclipse Public License v1.0
 * which accompanies this distribution, and is available at
 * http://www.eclipse.org/legal/epl-v10.html
 *
 * Contributors:
 *     IBM Corporation - initial API and implementation
 *******************************************************************************/
package edu.uts.reekie.designer.actions;

import java.util.List;

import org.eclipse.jface.resource.ImageDescriptor;
import org.eclipse.ui.IWorkbenchPart;

import org.eclipse.gef.EditPart;
import org.eclipse.gef.Request;
import org.eclipse.gef.commands.*;

import edu.uts.reekie.designer.*;
import edu.uts.reekie.designer.edit.*;
import edu.uts.reekie.designer.model.*;


public class SetConnectorDirectionAction
	extends org.eclipse.gef.ui.actions.SelectionAction
{

//
// Action IDs and Request Names
//

public static final String UNIDIRECTIONAL = "Set Connector to Unidirectional";
public static final String BIDIRECTIONAL = "Set Connector to Bidirectional";

protected static String generateRequestName(int dir)
{
	return generateId(dir);
}
protected static String generateId(int dir){ 
	switch (dir) {
	case AbstractConnector.BIDIRECTIONAL_CONNECTION:
		return BIDIRECTIONAL;
	case AbstractConnector.UNIDIRECTIONAL_CONNECTION:
		return UNIDIRECTIONAL;
	default:
		throw new IllegalArgumentException();
	}
}



Request request;
private int direction;

public SetConnectorDirectionAction(IWorkbenchPart part, int direction) {
	super(part);
	
	this.direction = direction;
	if (direction!=AbstractConnector.UNIDIRECTIONAL_CONNECTION && direction !=AbstractConnector.BIDIRECTIONAL_CONNECTION){
		throw new IllegalArgumentException("unknown connector direction " + direction);
	}
	
	
	request = new Request(generateRequestName(direction));
	
	// TODO: use messages instead of constants
	setText(generateId(direction));
	setToolTipText(generateId(direction));
	setId(generateId(direction));
	
	// TODO: change to real icon
	setImageDescriptor(
			ImageDescriptor.createFromFile(ReekieDesignerPlugin.class,"icons/plus.gif")); //$NON-NLS-1$
	
	
	setHoverImageDescriptor(getImageDescriptor());
}

protected boolean calculateEnabled() {
	return canPerformAction();
}

private boolean canPerformAction() {
	
	if (getSelectedObjects().isEmpty())
		return false;
	
	int notExpectedValue = direction;
	
	List<?> parts = getSelectedObjects();
	for (int i=0; i<parts.size(); i++){
		Object o = parts.get(i);
		if (!(o instanceof AbstractConnectorEditPart))
			return false;
		
		AbstractConnectorEditPart part = (AbstractConnectorEditPart)o;
		if (!(part.getModel() instanceof AbstractConnector))
			return false;
		
		AbstractConnector model = (AbstractConnector)part.getModel();
		
		if (model.getDirection()==notExpectedValue)
			return false;
		
	}
	return true;
}

private Command getCommand() {
	
	List<?> editparts = getSelectedObjects();
	CompoundCommand cc = new CompoundCommand();
	cc.setDebugLabel("Set Connector Direction");//$NON-NLS-1$
	for (int i=0; i < editparts.size(); i++) {
		EditPart part = (EditPart)editparts.get(i);
		cc.add(part.getCommand(request));
	}
	return cc;
}

public void run() {
	execute(getCommand());
}

}

